package cn.jiyun.service;

import cn.jiyun.entity.ResultClass;
import cn.jiyun.entity.day11.*;
import cn.jiyun.entity.day12.Contest;
import cn.jiyun.entity.day12.ContestDTO;
import cn.jiyun.entity.day12.ContestVO;
import cn.jiyun.entity.day15.Train;
import cn.jiyun.entity.day15.TrainDTO;
import cn.jiyun.entity.day9.Course;
import cn.jiyun.entity.day9.CourseDTO;
import cn.jiyun.entity.day9.CourseVO;
import cn.jiyun.service.fegin.*;
import cn.jiyun.utils.PageUtils;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author sjt
 * @Date 2023/8/31 11:42
 * @PackageName: cn.jiyun.service.fegin.EightDayService
 */
@Service
public class DayTenService {
    @Autowired
    TrainFeginService trainFeginService;
    @Autowired
    CourseFeginService courseFeginService;
    @Autowired
    ContestFeginService contestFeginService;
    @Autowired
    TestPaperFeginService testPaperFeginService;
    @Autowired
    DirectoryFeginService directoryFeginService;
    @Autowired
    UserFeginService userFeginService;

    @Autowired
    PageUtils pageUtils;
    @Autowired
    RedisTemplate redisTemplate;

    //查询用户历史测验次数
    public ResultClass findUserHistoryDetectionCount(Integer uid) {
        return ResultClass.ok(testPaperFeginService.findUserHistoryDetectionCount(uid));
    }

    //完善测试功能
    public ResultClass addTestPaperOverAnswers(Integer oid, String[] arr) {
        Map<Integer, String> map = new HashMap<>();
        for (String s : arr) {
            String[] split = s.split("#");
            map.put(Integer.parseInt(split[0]),split[1]);
        }
        return ResultClass.ok(testPaperFeginService.addTestPaperOverAnswers(oid,map));
    }
//查询当前登录人的课程集合
    public ResultClass selectLoginUserCourseList(ContestDTO dto, Integer uid) {
        ArrayList<CourseVO> list = new ArrayList<>();
        List<Integer> cids=trainFeginService.selectLoginUserCourseIds(uid);
            for (Integer cid : cids) {
//            查询一个课程
                CourseVO vo = new CourseVO();
                BeanUtils.copyProperties(courseFeginService.selectOne(String.valueOf(cid)), vo);
//            查询用户学习课程的进度
                Integer schedule = directoryFeginService.findUserCourseSchedule(cid, uid);
                vo.setSchedule(schedule);

                //如果传递了name判断当前名称是否包含,包含才添加
                if (StringUtil.isNotEmpty(dto.getCname())) {
                    if(vo.getCname().contains(dto.getCname())){
                        list.add(vo);
                    }
                    continue;
                }
                //直接添加
                list.add(vo);
            }
        return ResultClass.ok(pageUtils.pageHaper(list,dto.getPageNum(),dto.getPageSize()));
    }
//查询当前登录人的历史测验记录
    public ResultClass selectLoginUserTestPaperOverList(TestPaperDTO dto, Integer uid) {
        return testPaperFeginService.selectLoginUserTestPaperOverList(dto,uid);
    }
//查询历史测验所选的答案
    public ResultClass findhistoryTestPaperAnswer(Integer id) {
        List<TestpaperOverAnswer> list=testPaperFeginService.findhistoryTestPaperAnswer(id);
        return ResultClass.ok(list);
    }
//查询试卷
    public ResultClass selecttestpaper(Integer pid) {
        TestPaper testPaper=testPaperFeginService.selecttestpaper(pid);
        return ResultClass.ok(testPaper);
    }
//查询登录人的竞赛列表
    public ResultClass selectLoginUserContestList(Integer uid, ContestDTO contestDTO) {
//        List<Integer> cids=courseFeginService.selectLoginUserContestList(uid);
        PageInfo<Contest> pageInfo=contestFeginService.selectLoginUserContestList(uid,contestDTO);
        return ResultClass.ok(pageInfo);
    }
//查询登录人的培训列表
    public ResultClass selectLoginUserTrainList(Integer uid, TrainDTO trainDTO) {
        return ResultClass.ok(trainFeginService.selectLoginUserTrainList(uid,trainDTO));
    }
//修改登录人的头像
    public ResultClass changeLoginUserPictrue(Integer uid, String pictrue) {
        Integer i = userFeginService.changeLoginUserPictrue(uid, pictrue);
        if(i>0){
            return ResultClass.ok(1).Message("头像上传成功");
        }
        return ResultClass.error().Message("头像上传失败");
    }

    //更改密码
    public ResultClass changeLoginUserPassword(Integer uid, String password) {
        Integer i = userFeginService.changeLoginUserPassword(uid, password);
        if(i>0){
            return ResultClass.ok(1).Message("密码修改成功");
        }
        return ResultClass.error().Message("密码修改失败");
    }

    public ResultClass resettingLoginUserPassword(Integer uid) {
        Integer i = userFeginService.changeLoginUserPassword(uid, "123");
        if(i>0){
            return ResultClass.ok(1).Message("密码重置成功");
        }
        return ResultClass.error().Message("密码重置失败");
    }

    public ResultClass deleteTeam(Integer tid) {
        Integer i=userFeginService.deleteTeam(tid);
        if(i>0){
            return ResultClass.ok(1).Message("删除成功");
        }
        return ResultClass.error().Message("密码失败");
    }
}
